package com.cspg.ssss.service;

import com.alibaba.fastjson.JSONObject;
import com.cspg.ssss.config.WeixinProperties;
import com.cspg.ssss.domain.*;
import com.cspg.ssss.repository.DepartmentUserRepository;
import com.cspg.ssss.repository.GradeRoleUserMapper;
import com.cspg.ssss.repository.SuperAdminMapper;
import com.cspg.ssss.util.HttpRequest;
import com.cspg.ssss.vo.DepartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxDepartmentService {

    @Autowired
    WeixinService wxService;

    @Autowired
    WeixinProperties wxProperties;

    @Autowired
    DepartmentUserRepository departmentUserRepository;

    @Autowired
    SuperAdminMapper superAdminMapper;

    @Autowired
    GradeRoleUserService gradeRoleUserService;

    /**
     * 根据部门ID获取部门下所有子部门，如果部门ID=-1，则获取所有部门列表
     * @param did
     * @return
     */
    public JSONObject getDepartmentsList(int did){

        String token = wxService.getToken(wxProperties.getCorpid(),wxProperties.getSecret());
        String getUrl = wxProperties.getUrlroot()+"/cgi-bin/department/list?access_token=" + token;
        if(did > 0){
            getUrl += "&id="+String.valueOf(did);
        }

        JSONObject retobj = HttpRequest.httpGetRequest(getUrl);
        return retobj;
    }

    /**
     * 根据部门ID获取部门下所有人员列表
     * @param did 部门id
     * @param fetchChild 是否获取子部门人员 1-获取 0-不获取
     * @return
     */
    public JSONObject getUserListByDepartmentId(int did,int fetchChild){

        String token = wxService.getToken(wxProperties.getCorpid(),wxProperties.getSecret());
        String getUrl = wxProperties.getUrlroot()+"/cgi-bin/user/list?access_token="+token+"&department_id="+String.valueOf(did)+"&fetch_child="+String.valueOf(fetchChild);
        JSONObject retobj = HttpRequest.httpsGetRequest(getUrl);
        return retobj;
    }

    /**
     * 根据条件查询部门
     * @param condition key包括department_path department_name pid
     *                  其中department_path和department_name是模糊匹配
     *                  传入department_path 用户查询该部门下所有部门，包括子部门
     * @return
     */
    public List<DepartmentEntity> getDepartmentListByCondition(HashMap<String,Object> condition){
        return departmentUserRepository.findDepartmentByCondition(condition);
    }

    /**
     * 获取部门下人员详情
     * @param condition key包括 pid，department_path，
     *                  pid查询某个部门下单层人员
     *                  department_path查询某个部门下所有部门，包含子部门
     * @return
     */
    public List<UserDetailInfoEntity> getDepartmentUserList(HashMap<String,Object> condition){
        return departmentUserRepository.findDepartmentUsers(condition);
    }

    public UserEntity getUserInfoByUserId(String userid){
        return departmentUserRepository.findUserInfoByUserId(userid);
    }

    /**
     * 根据用户id 判断用户是否是超级管理员
     * @param userid
     * @return
     */
    public boolean isSuperAdmin(String userid){

        SuperAdmin admin = superAdminMapper.findSuperAdminById(userid);
        if(admin != null){
            return true;
        }

        return false;
    }

    public HashMap<String,Boolean> getUserRoles(String userid){
        List<GradeRoleUser> retList = gradeRoleUserService.getRoleListByUserid(userid);

        HashMap<String,Boolean> retMap = new HashMap<String,Boolean>();
        retMap.put("grade_approve",false);
        retMap.put("grade_user",false);
        retMap.put("depart_admin",false);

        if(retList != null && retList.size() > 0){
            for(int i=0;i<retList.size();i++){
                if(retList.get(i).getOption_type() == 0){
                    retMap.put("grade_approve",true);
                }
                if(retList.get(i).getOption_type() == 1){
                    retMap.put("depart_admin",true);
                }
                if(retList.get(i).getOption_type() == 2){
                    retMap.put("grade_user",true);
                }

            }
        }

        return retMap;
    }

    /**
     * 获取用户所在具体的科室（不支持一个用户在同一个部门下多个科室）
     * @param userid
     * @param dept_path
     * @return
     */
    public DepartVo getUserSubDept(String userid, String dept_path){
        DepartVo retsubDept = new DepartVo();
        HashMap<String,Object> usercondition = new HashMap<String, Object>();
        usercondition.put("userid",userid);
        List<UserDetailInfoEntity> userList = getDepartmentUserList(usercondition);
        if(userList != null && userList.size() > 0){
            for(int i=0;i<userList.size();i++){
                if(userList.get(i).getDepartment_displaypath().startsWith(dept_path)){
                    String dept_id_path = userList.get(i).getDepartment_path();
                    int index = dept_id_path.lastIndexOf("/");
                    String deptid = dept_id_path.substring(index+1);
                    int ndeptid = Integer.parseInt(deptid);
                    retsubDept.setId(ndeptid);
                    retsubDept.setDepartment_path(userList.get(i).getDepartment_displaypath());
                    return retsubDept;
                }
            }
        }
        return retsubDept;
    }


}
